﻿using AntdUI;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PosClient.Service
{
    public class ThemeHelper
    {
        /// <summary>
        /// 判断是否浅色
        /// </summary>
        /// <returns></returns>
        public static bool IsLightMode()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");
            if (key != null)
            {
                int appsUseLightTheme = (int)key.GetValue("AppsUseLightTheme", -1);
                if (appsUseLightTheme == 1)
                {
                    return true;
                }
                return false;
            }
            return true;
        }

        /// <summary>
        /// 设置明暗颜色
        /// </summary>
        /// <param name="window">父窗口</param>
        /// <param name="isLight">是否亮色</param>
        public static void SetColorMode(Window window, bool isLight)
        {
            if (isLight)
            {
                AntdUI.Config.IsLight = true;
                window.BackColor = Color.White;
                window.ForeColor = Color.Black;
            }
            else
            {
                AntdUI.Config.IsDark = true;// 设置为深色模式
                window.BackColor = Color.FromArgb(31, 31, 31);
                window.ForeColor = Color.White;
            }
            foreach (Control control in window.Controls)
            {
                SetColor(control, isLight);
            }
        }

        public static void SetColor(object controls, bool isLight)
        {
            if ((controls as Control).Controls.Count > 0)
            {
                foreach (Control control in (controls as Control).Controls)
                {
                    if (isLight)
                    {
                        control.BackColor =  Color.White;
                        control.ForeColor = Color.Black;
                        if (control.Controls.Count > 0)
                        {
                            SetColor(control, isLight);
                        }
                    }
                    else
                    {
                        control.BackColor = Color.FromArgb(31, 31, 31);
                        control.ForeColor = Color.White;
                        if (control.Controls.Count > 0)
                        {
                            SetColor(control, isLight);
                        }
                    }
                }
                if (isLight)
                {
                    if (controls is StackPanel)
                    {
                        (controls as StackPanel).BackColor =  Color.White;
                        (controls as StackPanel).ForeColor = Color.Black;
                    }
                }
                else
                {
                    if (controls is StackPanel)
                    {
                        (controls as StackPanel).BackColor =  Color.FromArgb(31, 31, 31);
                        (controls as StackPanel).ForeColor = Color.White;
                    }
                }
            }
        }

        public static void IconFillForm(Form form)
        {
            Random random = new Random();
            foreach (var control in form.Controls)
            {
                if (control is AntdUI.Label)
                {
                    if (string.IsNullOrEmpty((control as AntdUI.Label).PrefixSvg))
                    {
                        (control as AntdUI.Label).PrefixSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.Button)
                {
                    if (string.IsNullOrEmpty((control as AntdUI.Button).IconSvg))
                    {
                        (control as AntdUI.Button).IconSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.Input)
                {
                    if (string.IsNullOrEmpty((control as AntdUI.Input).PrefixSvg))
                    {
                        (control as AntdUI.Input).PrefixSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.Tag)
                {
                    if (string.IsNullOrEmpty((control as AntdUI.Tag).ImageSvg))
                    {
                        (control as AntdUI.Tag).ImageSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.PageHeader)
                {

                    if (string.IsNullOrEmpty((control as AntdUI.PageHeader).IconSvg))
                    {
                        (control as AntdUI.PageHeader).IconSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.FlowPanel || control is AntdUI.StackPanel
                                                || control is AntdUI.GridPanel || control is FlowLayoutPanel
                                                || control is AntdUI.Panel || control is UserControl
                                                || control is Control)
                {
                    IconFillControl(control as Control);
                }
            }
        }

        public static void IconFillControl(Control controls)
        {
            Random random = new Random();
            foreach (var control in controls.Controls)
            {
                if (control is AntdUI.Label)
                {
                    if (string.IsNullOrEmpty((control as AntdUI.Label).PrefixSvg))
                    {
                        (control as AntdUI.Label).PrefixSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.Button)
                {
                    if (string.IsNullOrEmpty((control as AntdUI.Button).IconSvg))
                    {
                        (control as AntdUI.Button).IconSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.Input)
                {
                    if (string.IsNullOrEmpty((control as AntdUI.Input).PrefixSvg))
                    {
                        (control as AntdUI.Input).PrefixSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.Tag)
                {
                    if (string.IsNullOrEmpty((control as AntdUI.Tag).ImageSvg))
                    {
                        (control as AntdUI.Tag).ImageSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.PageHeader)
                {

                    if (string.IsNullOrEmpty((control as AntdUI.PageHeader).IconSvg))
                    {
                        (control as AntdUI.PageHeader).IconSvg = DataUtil.IconList[random.Next(DataUtil.IconList.Count)];
                    }
                }
                if (control is AntdUI.FlowPanel || control is AntdUI.StackPanel
                                                || control is AntdUI.GridPanel || control is FlowLayoutPanel
                                                || control is AntdUI.Panel || control is UserControl
                                                || control is Control)
                {
                    IconFillControl((control as Control));
                }
            }
        }
    }
}
